package com.szewec.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.szewec.common.util.PageInfo;
import com.szewec.common.util.ResponseUtil;
import com.szewec.model.vo.UserModel;
import com.szewec.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user/")
@Api(value = "用户",description = "用户控制层")
public class UserController {

    @Autowired
    private IUserService userService;
    private Logger logger = LoggerFactory.getLogger(UserController.class);

    @ApiOperation(value = "分页查询用户列表")
    @RequestMapping(value = "/gets",method = RequestMethod.GET)
    public Object getListData(@RequestParam(required = false) int pageIndex,
                              @RequestParam(required = false) int pageSize,
                              HttpServletRequest request){
        try{
            EntityWrapper<UserModel> userEW = new EntityWrapper<UserModel>();
            String name    = request.getParameter("name");
            String gtAge   = request.getParameter("gtAge");
            String ltAge   = request.getParameter("ltAge");
            String sex     = request.getParameter("sex");
            String address = request.getParameter("address");
            if(StringUtils.isNotEmpty(name)){
                userEW.like("name",name);
            }
            if(StringUtils.isNotEmpty(gtAge)){
                userEW.gt("age",Integer.valueOf(gtAge));
            }
            if(StringUtils.isNotEmpty(ltAge)){
                userEW.lt("age",Integer.valueOf(ltAge));
            }
            if(StringUtils.isNotEmpty(sex)){
                userEW.eq("sex",sex);
            }
            if(StringUtils.isNotEmpty(address)){
                userEW.like("address",address);
            }
            PageInfo<UserModel> userPage = userService.selectPage(
                    new Page<UserModel>(pageIndex,pageSize),
                    userEW
            );
            return ResponseUtil.successListResponse(userPage);
        }catch (Exception e){
            logger.error(e.getMessage());
            return ResponseUtil.failedResponse("查询失败",e.getMessage());
        }

    }
    @ApiOperation(value = "创建一个用户")
    @RequestMapping(value = "/posts",method = RequestMethod.POST)
    public Object createObject(@ModelAttribute UserModel user){
        try{
            if(StringUtils.isBlank(user.getFid())) {
                int i=(int)(Math.random()*900)+100;
                user.setFid(String.valueOf(i));
            }
            userService.insert(user);
            return ResponseUtil.successResponse("新增成功");
        }catch (Exception e){
            logger.error(e.getMessage());
            return ResponseUtil.failedResponse("新增失败",e.getMessage());
        }

    }

    @ApiOperation(value = "删除用户",notes = "删除一个或多个用户，多个用户id以逗号隔开")
    @RequestMapping(value = "/deletes")
    public Object deleteObjects(@RequestParam("ids") String ids){
        try{
            userService.delete(ids);
            return ResponseUtil.successResponse("删除成功");
        }catch (Exception e){
            logger.error(e.getMessage());
            return ResponseUtil.failedResponse("删除失败",e.getMessage());
        }
    }

    @ApiOperation(value = "根据ID修改用户信息")
    @RequestMapping(value = "/update-by-id", method = RequestMethod.POST)
    public Object updateObjectById(@ModelAttribute UserModel user){
        try{
            userService.update(user);
            return ResponseUtil.successResponse("修改成功");
        }catch (Exception e){
            logger.error(e.getMessage());
            return ResponseUtil.failedResponse("修改失败",e.getMessage());
        }
    }
}
